package config

import (
	"BingWallpaper/logger"
	"fmt"
	"os"
	"os/user"

	"gopkg.in/ini.v1"
)

var cfg *ini.File

func init() {
	inicfg, err := ini.Load("./config/config.ini")
	if err != nil {
		fmt.Printf("Fail to read file: %v", err)
		os.Exit(1)
	}
	cfg = inicfg
}

//得到图片保存路径
func GetPicPath() string {
	userinfo, err := user.Current()
	if err != nil {
		logger.Logerr(fmt.Sprint(err))
	}
	defaultDir := userinfo.HomeDir + "/BingWallpaper/"
	logger.Loginfo("缺省目录：" + defaultDir)

	if cfg.Section("paths").HasKey("pic_path") {
		defaultDir = cfg.Section("paths").Key("pic_path").String()
		logger.Loginfo("设置目录为:" + defaultDir)
	}
	return defaultDir
}

func GetMaxIdx() int {
	if cfg.Section("length").HasKey("max_idx") {
		max_idx, err := cfg.Section("length").Key("max_idx").Int()
		if err == nil {
			return max_idx
		}
	}
	return 1
}
